<template>
  <!-- 登录大背景 -->
  <div class="login-container">
    <!-- 登录窗口 -->
    <div class="login-box">
      <!-- 头部logo -->
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <!-- 表单输入 -->
      <el-form :model="user" :rules="rules" ref="ruleForm">
        <el-form-item prop="mobile">
          <el-input v-model="user.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="user.code" placeholder="请输入验证码"></el-input>
        </el-form-item>
        <el-form-item class="test" prop="isChecked">
          <el-checkbox v-model="user.isChecked"
            >我已阅读并同意用户协议和隐私条款</el-checkbox
          >
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="default" @click="validateLogin"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { userLogin } from '@/api/user'
import { setToken } from '@/utils/store'
export default {
  data () {
    // 定义校验函数
    const validateCode = (rule, value, callback) => {
      // console.log(rule, value, callback)
      if (!value) {
        callback(new Error('验证码不能为空！'))
      } else if (value.length !== 6) {
        callback(new Error('验证码长度为6位！'))
      } else {
        // 通过验证
        callback()
      }
    }

    const validateCheck = (rule, value, callback) => {
      // console.log(rule, value, callback)
      if (!value) {
        callback(new Error('请勾选同意协议！'))
      } else {
        // 通过验证
        callback()
      }
    }

    return {
      // 表单数据
      user: {
        mobile: '13911111111',
        code: '246810',
        isChecked: false
      },
      // 表单验证规则
      rules: {
        mobile: [
          // 是否为空
          { required: true, message: '请输入手机号', trigger: 'blur' },
          // 手机号长度
          { min: 11, max: 11, message: '长度11字符', trigger: 'blur' }
        ],
        code: [
          // 自定义函数校验
          { validator: validateCode, trigger: 'blur' }
        ],
        isChecked: [
          // 自定义函数校验
          { validator: validateCheck, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 验证表单
    validateLogin () {
      this.$refs.ruleForm.validate((validate) => {
        if (validate) {
          this.login()
        } else {
          this.$message({
            type: 'warning',
            message: '验证失败！'
          })
          return false
        }
      })
    },
    async login () {
      const { mobile, code } = this.user
      const { status, data: { data } } = await userLogin(mobile, code)
      // console.log(res)
      if (status === 201) {
        setToken(data)
        this.$router.replace('/')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  // right: 0;
  // bottom: 0;
  width: 100%;
  height: 100%;
  background: url("../../assets/img/login_bg.jpg") no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-box {
    min-width: 300px;
    padding: 30px 50px;
    background: #fff;
    box-shadow: 5px 8px 8px #333;
    border-radius: 20px;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 60px;
        background: url("../../assets/img/logo_index.png") no-repeat;
        background-size: contain;
      }
    }

    /deep/ .el-form-item__content {
      text-align: left;
    }
    // .test {
    //   text-align: left;
    // }
    .el-button {
      width: 100%;
    }
  }
}
</style>
